package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.entity.SpcyRsIcon;
import com.yuncheng.spcyApi.entity.SpcyRsNotice;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyRsIconMapper;
import com.yuncheng.spcyApi.service.ISpcyRsIconService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 审评查验-icon图标库
 * @Author: yuncheng
 * @Date: 2023-11-09
 * @Version: V1.0
 */
@Service
public class SpcyRsIconServiceImpl extends BaseServiceImpl<SpcyRsIconMapper, SpcyRsIcon> implements ISpcyRsIconService {

    private static final Logger log = LoggerFactory.getLogger(SpcyRsIconServiceImpl.class);

    @Override
    public IPage<SpcyRsIcon> queryPageIcon(SpcyRsIcon icon, Integer pageNo, Integer pagSize) {
        Page<SpcyRsIcon> page = new Page<>(pageNo,pagSize);

        LambdaQueryWrapper<SpcyRsIcon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(icon.getFcode()), SpcyRsIcon::getFcode, icon.getFcode());
        queryWrapper.like(StringUtils.isNotBlank(icon.getFname()), SpcyRsIcon::getFname, icon.getFname());
        queryWrapper.like(StringUtils.isNotBlank(icon.getFyxzt()), SpcyRsIcon::getFyxzt, icon.getFyxzt());
        queryWrapper.orderByDesc(SpcyRsIcon::getCreateTime);
        IPage<SpcyRsIcon> pageList = page(page, queryWrapper);

        return pageList;
    }

    @Override
    public List<SpcyRsIcon> queryListIcon(SpcyRsIcon icon) {

        LambdaQueryWrapper<SpcyRsIcon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(icon.getFcode()), SpcyRsIcon::getFcode, icon.getFcode());
        queryWrapper.like(StringUtils.isNotBlank(icon.getFname()), SpcyRsIcon::getFname, icon.getFname());
        queryWrapper.like(StringUtils.isNotBlank(icon.getFyxzt()), SpcyRsIcon::getFyxzt, icon.getFyxzt());
        queryWrapper.orderByAsc(SpcyRsIcon::getCreateTime);

        List<SpcyRsIcon> list = list(queryWrapper);
        return list;
    }

    @Override
    public SpcyRsIcon addIcon(SpcyRsIcon icon) {
        icon.setId(GetUuIdUtils.ReplaceUuId());
        if (StringUtils.isBlank(icon.getFcode())){
            throw new BusinessException("编码为空！");
        }

        List<SpcyRsIcon> existsList = list(new LambdaQueryWrapper<SpcyRsIcon>().eq(SpcyRsIcon::getFcode, icon.getFcode()));
        if (CollectionUtil.isNotEmpty(existsList)){
            throw new BusinessException("当前编码已经存在！");
        }

        if (StringUtils.isBlank(icon.getFname())){
            throw new BusinessException("名称为空！");
        }
        save(icon);
        return icon;
    }

    @Override
    public void updateIcon(HashMap<String,Object> paramMap) {
        String id = paramMap.get("id").toString();
        SpcyRsIcon updateIcon = getById(id);
        String oldCode = updateIcon.getFcode();
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(updateIcon, paramMap);
        } catch (IllegalAccessException e) {
            log.error("更新icon图标出错：[IllegalAccessException]->{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新icon图标出错：[InvocationTargetException]->{}",e.getMessage());
        }

        if (StringUtils.isBlank(updateIcon.getFcode())){
            throw new BusinessException("编码为空！");
        }

        if (StringUtils.isBlank(updateIcon.getFname())){
            throw new BusinessException("名称为空！");
        }

        if (!updateIcon.getFcode().equals(oldCode)) {
            List<SpcyRsIcon> existsList = list(new LambdaQueryWrapper<SpcyRsIcon>().eq(SpcyRsIcon::getFcode, updateIcon.getFcode()));
            if (CollectionUtil.isNotEmpty(existsList)) {
                throw new BusinessException("当前编码已经存在！");
            }
        }

        updateById(updateIcon);
    }

    @Override
    public void deleteIcon(ParamsBo paramsBo) {
        if (StringUtils.isBlank(paramsBo.getIds())){
            throw new BusinessException("请选择数据！");
        }

        List<String> idsList = StringPatternUtils.getListByIds(paramsBo.getIds());
        removeByIds(idsList);
    }
}
